package router

import (
	"github.com/gin-gonic/gin"
	"go-sec-code/database"
	"net/http"
)

type ConnStrHandler struct {
}

func NewConnStrHandler() *ConnStrHandler {
	return &ConnStrHandler{}
}

func (handler *ConnStrHandler) RegisterRoutes(r *gin.RouterGroup) {
	r.GET("/conn_str", handler.ConnStr)
}

func (handler *ConnStrHandler) ConnStr(ctx *gin.Context) {
	username := ctx.Query("username")
	password := ctx.Query("password")
	host := ctx.Query("host")
	port := ctx.Query("port")

	mysqlUser := database.NewMysqlUser()
	_, err := mysqlUser.ConnectDBCustom(username, password, host, port)
	if err != nil {
		ctx.String(http.StatusBadRequest, "Error: %v", err)
		return
	}

	ctx.String(http.StatusOK, "conn succ")
}